import webview
import time
import json

# 目标 URL
url = "https://chatgpt.com"

# 自动化操作函数
def automate(window):
    time.sleep(2)  # 等待页面加载完成
    # 示例：点击一个按钮（假设按钮的ID为'someButton')
    while True:
        pass
    js_code = """
        document.getElementById('someButton').click();
    """
    #window.evaluate_js(js_code)

# 自动化操作函数
def save_cookies(window):
    time.sleep(5)  # 等待登录完成并加载页面
    cookies = window.evaluate_js('document.cookie')
    with open('cookies.json', 'w') as f:
        json.dump(cookies, f)
# 自动化操作函数
def load_cookies(window):
    with open('cookies.json', 'r') as f:
        cookies = json.load(f)
    js_code = f'document.cookie = "{cookies}";'
    window.evaluate_js(js_code)
    time.sleep(2)  # 等待 Cookie 设置完成
    window.load_url(url)  # 重新加载页面以反映登录状态

# 创建窗口函数
def create_window():
    window = webview.create_window('WebView2 Automation Example', url)
    webview.start(automate, window)

# 启动程序
if __name__ == '__main__':
    create_window()
